package zhcms.framework;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ActionServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6228687325918759930L;

	/**
	 * Constructor of the object.
	 */
	
	public ActionServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//System.out.println("start1");
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//System.out.println("start2");
		
		
		//取得上下文路径
		String contextPath=request.getContextPath();
		//System.out.println(contextPath);
		//取得请求的url
		String uri=request.getRequestURI();
		String actionPath="";
		if(contextPath == null || contextPath.length() < 1)
		{
			if(uri == null || uri.length() < 1)
			{
				actionPath="";
			}
			else
			{
				actionPath=uri.substring(1,uri.length());
			}
		}
		else
		{
			if(uri == null || uri.length() < 1)
			{
				actionPath="";
			}
			else
			{
				actionPath=uri.substring(contextPath.length(),uri.length());
			}
		}
		//System.out.println(actionPath.substring(0,1));
		if(actionPath.substring(0,1).equals("/"))
		{
			actionPath=actionPath.substring(1);
		}
		String temp_url="";
		if(actionPath == null || actionPath.length() < 1)
		{
			//定向到错误页面
		}
		else
		{
			GySplit a=new GySplit();
			a.tool(actionPath, "!");
			String[] temp_array=a.Show();
			a=null;
			for(int i=0;i<(temp_array.length-1);i++)
			{
				temp_url=temp_url+temp_array[i]+".";
			}
			
			GySplit b=new GySplit();
			b.tool(temp_array[(temp_array.length-1)], ".");
			String[] temp_method=b.Show();
			b=null;
			String temp_classname=temp_method[0];
			GySplit c=new GySplit();
			c.tool(temp_classname, "(");
			String[] temp_methodname=c.Show();
			c=null;
			temp_url=temp_url+temp_methodname[0];
			String t_methodname="";
			t_methodname=temp_methodname[1].replace(")", "");
			System.out.println("temp_url++++"+temp_url);
			try {
				Action action=(Action) Class.forName(temp_url).newInstance();
				Object obj = Class.forName(temp_url).newInstance();  
				Method method = action.getClass().getMethod(t_methodname,new Class[] { HttpServletRequest.class,HttpServletResponse.class } );
				//System.out.println("method++++"+t_methodname.toString());
				ActionForward actionForward=(ActionForward) method.invoke(obj, new Object[] {request, response });
				//ActionForward actionForward=action.t_methodname(request, response);
				//System.out.println("ReIsOk++++"+actionForward.ReIsOk());
				if(actionForward.ReIsOk()==1){
					response.sendRedirect(contextPath+actionForward.getForwardPath());
				}
				else if(actionForward.ReIsOk()==2)
				{
					RequestDispatcher dispatcher=request.getRequestDispatcher(actionForward.getForwardPath());
					dispatcher.forward(request, response);
				}
				else if(actionForward.ReIsOk()==3)
				{
					PrintWriter out = response.getWriter();
					//打印带调转的js
					out.println("<script language='javascript'>alert('" + actionForward.getTishi() +"');location.href=\"" + actionForward.getForwardPath() +"\";</script>");
					//打印后退的js
					
				}
				else if(actionForward.ReIsOk()==4)
				{
					//返回上一页
					PrintWriter out = response.getWriter();
					//打印带调转的js
					out.println("<script language='javascript'>alert('" + actionForward.getTishi() +"');history.go("+actionForward.getRedirectHistory()+");</script>");
					//打印后退的js
				}
				else
				{
					
				}
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (InstantiationException e) {
				e.printStackTrace();
			}
		}

	}

	public void init(ServletConfig config) throws ServletException {
		// Put your code here
		//System.out.println("start");
	}

}
